package com.highcom.service.impl;

import com.highcom.mapper.MemberDao;
import com.highcom.model.Member;
import com.highcom.service.MemberService;
import com.highcom.utils.MD5Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/8/7 15:08
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberDao memberDao;


    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        //将密码进行MD5加密
        String password = member.getPassword();
        if(password != null){
            member.setPassword(MD5Utils.md5(password));
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonths(ArrayList<String> months) {
        ArrayList<Integer> list = new ArrayList<>();
        for (String m : months) {
            String[] split = m.split("\\.");
            String year = split[0];
            String month1 = split[1];
            int days = getMonthDays(Integer.parseInt(year), Integer.parseInt(month1));

            m = m+"."+days;
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);

        }
        return list;
    }

    public static int getMonthDays(int year, int month) {
        if (month == 2) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                return 29;
            } else {
                return 28;
            }
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        } else {
            return 31;
        }
    }
}
